﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMTVision.WidgetWeb.WidgetContainers
{
    public partial class WidgetContainer : CMTVision.Widgets.Framework.BaseWidgetContainer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(); 
            }              
        }

        #region Override

        public override Control WidgetHolder
        {
            get
            {
                return widgetHolder;
            }
        }

        public override Control ContainerWrapper
        {
            get
            {
                return MainContainerWrapper;
            }
        } 

        #endregion

        #region Bind Data

        protected void BindData()
        {
            approveCircleWidgetAnchor.Visible = IsInEditMode;
            deleteWidgetAnchor.Visible = IsInEditMode;
            editWidgetAnchor.Visible = IsInEditMode;
            editWidgetAnchor.Attributes["class"] = Widget.IsInEditting ? "editwidget widget_closeedit" : "editwidget widget_openedit";
            saveWidgetAnchor.Visible = Widget.IsInEditting;

            switch (TitleShowMode)
            {
                case CMTVision.Widgets.Framework.TitleShowMode.ShowOnFocus:
                    {
                        widgetHeader.Attributes["class"] = "widgetHeader hide";
                        //showHeaderDiv.Attributes["class"] = "showHeaderDiv";
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        #endregion

        #region Edit Button Event Handle

        protected void editAnchor_Click(object sender, EventArgs e)
        {
            if (Widget.IsInEditting)
            {
                editWidgetAnchor.Attributes["class"] = "editwidget widget_openedit";
                Widget.IsInEditting = false;
                Widget.Refresh();
            }
            else
            {
                editWidgetAnchor.Attributes["class"] = "editwidget widget_closeedit";
                Widget.Edit(); 

            }
            saveWidgetAnchor.Visible = Widget.IsInEditting;
            widgetContainerUpdatePanel.Update();
        }

        protected void collapseAnchor_Click(object sender, EventArgs e)
        {
            bool inExpendStatus = collapseWidgetAnchor.Attributes["class"].IndexOf("widget_collapse") >= 0;
            collapseWidgetAnchor.Attributes["class"] = inExpendStatus ? "editwidget widget_expend" : "editwidget widget_collapse";
            widgetHolder.Visible = !inExpendStatus;
            widgetContainerUpdatePanel.Update();
        }

        protected void deleteAnchor_Click(object sender, EventArgs e)
        {
            Widget.Delete(); 
            Deleted(this, Widget);             
        }

        protected void saveAnchor_Click(object sender, EventArgs e)
        {
            Widget.Save();
            BindData();
            widgetContainerUpdatePanel.Update();
        }

        protected void lockAnchor_Click(object sender, EventArgs e)
        {
            string lockAnchorCss = " " + lockWidgetAnchor.Attributes["class"] + " ";
            string mainContainerCss = " " + MainContainerWrapper.Attributes["class"] + " ";
            bool isLocked = lockAnchorCss.Contains(" widget_unlock ");

            lockWidgetAnchor.Attributes["class"] = isLocked ? "editwidget widget_lock" : "editwidget widget_unlock";
            MainContainerWrapper.Attributes["class"] = (isLocked ? mainContainerCss.Replace("locked", "") : mainContainerCss + "locked").Trim();

            var parent = MainContainerWrapper.Parent;
            while (!(parent == null || parent is UpdatePanel))
            {
                parent = parent.Parent;
            }
            if (parent is UpdatePanel)
            {
                (parent as UpdatePanel).Update();
            } 
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            //ScriptManager.RegisterStartupScript(widgetContainerUpdatePanel, widgetContainerUpdatePanel.GetType(), widgetContainerUpdatePanel.ClientID + "_Lock",
            //    String.Format("$('#{0}').click(function(){{var isLocked=$(this).hasClass('widget_unlock'); if(isLocked){{$(this).removeClass('widget_unlock').addClass('widget_lock');$(this).parents('.widgetContainer').removeClass('locked');}}else{{$(this).removeClass('widget_lock').addClass('widget_unlock');$(this).parents('.widgetContainer').addClass('locked');}} }});", lockWidgetAnchor.ClientID).ToString(), true);
            base.OnPreRender(e);
        }
         
    }
}